package com.cqcomponents.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import com.cqcomponents.api.components.IComponent;
import com.cqcomponents.api.exceptions.ComponentNotFoundException;
import com.cqcomponents.api.exceptions.MandatoryComponentException;

public class ExposeComponentTag extends AbstractComponentTag {

	@Override
	public void doTag() throws JspException, IOException {

		IComponent component = null;

		try {
			component = getComponentManager().getComponentFromResource(
					getResource());
		} catch (MandatoryComponentException e) {
			this.logger.warn(e.getMessage(), e);
		}

		if (component == null) {
			this.logger
					.info("No component bean found for resource at: "
							+ this.getResource()
							+ ". Please make sure that the resource has a resource type and it is "
							+ "wrapped within a ComponentBean tag. Running without component bean support.");
			throw new JspException(
					new ComponentNotFoundException(getResource()));
		} else {
			this.getPageContext().setAttribute("c", component);
		}
	}
}